package com.shujia.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

public class Code05GetChildren {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181"
                , 10 * 1000
                , new Watcher() {
            @Override
            // 该方法在Zookeeper连接中会被触发两次，一次在建立连接时，另外一次在关闭连接时
            // 该方法执行时，是按照异步执行
            public void process(WatchedEvent event) {
                System.out.println("当前Zookeeper连接被触发...");
            }
        }
        );
        // public List<String> getChildren(final String path, Watcher watcher)

        // public List<String> getChildren(final String path, Watcher watcher,
        //            Stat stat)
        List<String> children = zooKeeper.getChildren("/api", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("监控获取子节点信息" + event.getType());
            }
        });


//        for (String child : children) {
//            System.out.println("child:"+child);
//        }

        // 获取子节点时，不需要进行监控
        List<String> children1 = zooKeeper.getChildren("/api", false);
        for (String child : children1) {
            System.out.println("child:"+child);
        }

        zooKeeper.close();

    }
}
